package com.ztsoft.user.common.exception;

import com.ztsoft.user.pojo.enums.ResultCode;
import com.ztsoft.user.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理 Exception 异常
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result<Object> exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        log.error("服务错误:", e);
        if (e instanceof CustomException) {
            CustomException sysException = (CustomException) e;
            return Result.returnMsg("请联系管理员", sysException.getCode());
        } else {   // 未知问题
            return Result.returnMsg("请联系管理员", ResultCode.FAILED.getCode());
        }
    }
}